package com.uwo.house.kits;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yanhao on 2016/8/11.
 * 正则验证
 */
public class MatcherKit {

    private static final String matcher_email = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";

    //private static final String matcher_mobile = "^((1[34578][0-9]))\\d{8}$";
    //private static final String matcher_mobile = "^((13[0-9])(15[0-9])(17[0-9])(18[0-9]))\\d{8}$";
    //private static final String matcher_mobile = "^((13\\d{9}$)|(14\\d{9}$)|(15\\d{9}$)|(16\\d{9}$)|(17\\d{9}$)|(18\\d{9}$))";
    private static final String matcher_mobile = "^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$";

    private static final String matcher_cert= ".+(.p12)$";

    private static final String matcher_image= ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$";

    private static final String matcher_urlIsParmas = "http(s)?://(([\\w-]+\\.)+)[\\w-/.]+[?]([\\w- ./%&=\\u4e00-\\u9fa5]*)?";
//    private static final String matcher_urlIsParmas = "http(s)?://((([\\w-]+]+\\.)+)|(localhost))([:\\d]+)[\\w-/.]+[?]([\\w- ./%&=]*)?";
    //private static final String matcher_urlIsParmas =  "http(s)?://((([\\w-]+\\.)+)|(localhost)|(localhost[:\\d]+))[\\w-/.]+[?]([\\w- ./%&=]*)?";
    //private static final String matcher_urlIsParmas =  "http(s)?://((([\\w-]+\\.)+)|(localhost)|(localhost[:\\d]+))[\\w-/.]+[?]([\\w- ./%&=]*)?";

    /**
     * 邮箱验证
     * @param email
     * @return
     */
    public static boolean isEmail(String email){
        return matchers(matcher_email, email);
    }

    /**
     * 手机验证
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile){
        return matchers(matcher_mobile, mobile);
    }

    /**
     * 判断图片的类型
     * @param fileName
     * @return
     */
    public static boolean isCertType(String fileName){
        return matchers(matcher_cert, fileName);
    }

    /**
     * 判断图片的类型
     * @param fileName
     * @return
     */
    public static boolean isImageType(String fileName){
        return matchers(matcher_image, fileName);
    }

    /**
     * url
     * @param url
     * @return
     */
    public static boolean isUrlParmas(String url){
        int idx = url.indexOf("?");
        if(idx>0){
            return true;
        }
        else{
            return false;
        }
        //return matchers(matcher_urlIsParmas, url);
    }

    public static boolean matchers(String matcher, String str){
        Pattern p = Pattern.compile(matcher);
        Matcher m = p.matcher(str);
        return m.matches();
    }

}
